package com.example.myactspringboot;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

/**
 * @Author Zhouchb
 * @Create 2023-10-27 22:06
 * @Description
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyActSpringBootApplication.class)
public class all_key_gateway {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("processes/haa.bpmn20.xml") // 加载流程资源文件
                .name("multiInstance流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("multiInstance"); // 流程定义表的KEY字段值
        //启动流程并分配给
        Map<String,Object> map = new HashMap<>();
        List<String> assigneeList = new ArrayList<String>();
        assigneeList.add("boss");
        assigneeList.add("leader2");
        assigneeList.add("leader3");
        map.put("persons", assigneeList);
        ProcessInstance pi = runtimeService.startProcessInstanceById("ha:12:522535",map); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    @Test
    public void comleteProcess(){
        Task task = taskService.createTaskQuery()
                //ACT_RU_TASK 表中 PROC_INST_ID_cdbf8217-de81-11ec-8925-666ee0fc370d
                .processInstanceId("540001")
                .taskAssignee("boss")
                .singleResult();
        if (task!=null){
            Map<String,Object> map = new HashMap<>();
            Integer reject = (int)taskService.getVariable(task.getId(),"agree");
            map.put("power",false);
            map.put("agree",2);
            taskService.complete(task.getId(),map);
            System.out.println("完成");
        }
    }

    //不同意
    @Test
    public void comleteProcess2(){
        Task task = taskService.createTaskQuery()
                //ACT_RU_TASK 表中 PROC_INST_ID_cdbf8217-de81-11ec-8925-666ee0fc370d
                .processInstanceId("415001")
                .taskAssignee("leader3")
                .singleResult();
        if (task!=null){
            Map<String,Object> map = new HashMap<>();
            Integer reject = (int)taskService.getVariable(task.getId(),"agree");
            map.put("agree",reject+1);
            taskService.complete(task.getId(),map);
        }
    }
}
